package com.logistics.account.controller;

import com.logistics.account.entity.model.AccountPager;
import com.logistics.account.entity.model.AccountSave;
import com.logistics.account.entity.model.LoginModel;
import com.logistics.account.service.AccountService;
import com.logistics.utils.aspect.annotation.LoggerListener;
import com.logistics.utils.constants.LoggerMethod;
import com.logistics.utils.exception.MessageException;
import com.logistics.utils.response.ResponseUtil;
import com.logistics.utils.tool.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/sys/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private UserUtils userUtils;

    /**
     * 用户登录
     * @param model
     * @return
     * @throws Exception
     */
    @PostMapping("/login")
    @LoggerListener(businessName = "登录", methodName = LoggerMethod.SEARCH)
    public ResponseEntity login(@RequestBody LoginModel model) throws Exception {
        Map<String,String> res = accountService.loginCheck(model);
        return ResponseUtil.success(res);
    }

    /**
     * 获取api登录token
     * @param code
     * @return
     * @throws Exception
     */
    @GetMapping("/getToken/{code}")
    public ResponseEntity getToken(@PathVariable("code") String code) throws Exception {
        return ResponseUtil.success(accountService.apiToken(code));
    }

    /**
     * token检查
     * @param token
     * @return
     */
    @GetMapping("/checkToken/{token}")
    @LoggerListener(businessName = "检查token", methodName = LoggerMethod.SEARCH)
    public ResponseEntity checkToken(@PathVariable("token") String token) {
        return ResponseUtil.success(accountService.checkToken(token));
    }

    /**
     * 修改密码
     * @param password
     * @return
     */
    @PutMapping("/modifyPassword/{password}")
    public ResponseEntity modifyPassword(@PathVariable("password") String password){
        accountService.updatePassword(password);
        return ResponseUtil.success();
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("userInfo")
    @LoggerListener(businessName = "获取用户信息", methodName = LoggerMethod.SEARCH)
    public ResponseEntity userInfo(){
        return ResponseUtil.success(userUtils.getUserInfo());
    }

    /**
     * 分页查询
     * @param param
     * @return
     */
    @PostMapping("/pager")
    @LoggerListener(businessName = "分页查询用户信息", methodName = LoggerMethod.SEARCH)
    public ResponseEntity pager(@RequestBody AccountPager param){
        return ResponseUtil.success(accountService.pager(param));
    }

    /**
     * 保存
     * @param save
     * @return
     */
    @PutMapping("save")
    @LoggerListener(businessName = "保存用户信息", methodName = LoggerMethod.SAVE)
    public ResponseEntity save(@RequestBody AccountSave save) throws MessageException {
        accountService.save(save);
        return ResponseUtil.success();
    }

    /**
     * 查询详细
     * @param id
     * @return
     * @throws MessageException
     */
    @GetMapping("details/{id}")
    @LoggerListener(businessName = "查询用户详细信息", methodName = LoggerMethod.SEARCH)
    public ResponseEntity details(@PathVariable("id") String id) throws MessageException {
        return  ResponseUtil.success(accountService.details(id));
    }

    /**
     * 删除
     * @param ids
     * @return
     * @throws MessageException
     */
    @DeleteMapping("/remove/{ids}")
    @LoggerListener(businessName = "删除用户信息", methodName = LoggerMethod.DELETE)
    public ResponseEntity remove(@PathVariable("ids") String ids) throws MessageException {
        accountService.delete(ids);
        return  ResponseUtil.success();
    }


}
